﻿using System;
using System.Collections.Generic;
using System.Linq;
using DotNet.Common.Utilities.Comparer;

namespace DotNet.Common.Extensions.Linq
{
    /// <summary>
    /// Linq扩展类
    /// </summary>
    public static class LinqExtensions
    {
        /// <summary>
        /// Distinct 扩展方法,用于选择属性比较
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="V"></typeparam>
        /// <param name="source"></param>
        /// <param name="keySelector"></param>
        /// <returns></returns>
        public static IEnumerable<T> Distinct<T, V>(this IEnumerable<T> source, Func<T, V> keySelector)
        {
            return source.Distinct(new CommonEqualityComparer<T, V>(keySelector, null));
        }

        /// <summary>
        /// 按个分组
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="soure"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static ILookup<int, T> ToGroup<T>(this IEnumerable<T> soure, int count)
        {
            return soure.Select((x, i) => new { index = i, value = x }).ToLookup(x => (x.index / count), x => x.value);
        }
    }
}
